// ==UserScript==
// @name           MIS Worklist Alterations
// @namespace      https://mist.mitra.com
// @description    Adjust things in the MIS to make it look better
// @include        *mist.mitra.com/MISCore/*
// ==/UserScript==

// Change the Title of the worklist to accurately reflect what
// worklist it is we are looking at
var headings = document.getElementsByTagName( 'H2' );
for( var i = 0; i < headings.length; i++ )
{
	if( headings[i].getAttribute('align') == "center" )
	{
		var teamName = headings[i].getElementsByTagName( 'FONT' );
 		document.title = teamName[0].innerHTML + ' Worklist';
	}
}


// Make the worklists look better instead of having the Summary Line 
// not colspan the whole width of the table.
var tableCells = document.getElementsByTagName( 'TD' );
for( var j = 0; j < tableCells.length; j++ )
{
	if( tableCells[j].getAttribute('colspan') == "12" &&
	    tableCells[j].innerHTML.indexOf('Summary:') >= 0 )
	{
		tableCells[j].colSpan = "13";
	}
	
	// Add links to the Issue Number when an issue 
	// was Linked or Unlinked in the Issue History
	if( tableCells[j].getAttribute('colspan') == "4" &&
	    tableCells[j].getAttribute('bgcolor') == "#dddd99" &&
	   (tableCells[j].innerHTML.indexOf('Issue(s) ') >=0 ||
	    tableCells[j].innerHTML.indexOf('Linked issue(s) ') >= 0))
	{
		var misRE = /(^|[^0-9])([0-9]{5,6})([^0-9]|$)/ig;
		//var misRE = /\b([0-9]{5,6})\b/g;

		var cellContents = tableCells[j].innerHTML;

		var matches = cellContents.match(misRE);

		if( matches != null )
		{
			tableCells[j].innerHTML = cellContents.replace(misRE, function (str, p1, p2, p3, offset, s) {
			return p1 + p2.link("https://mist.mitra.com/MISCore/importsissue/Issue.asp?Ref=" + p2) + p3; });
		}
	}
	
	// Add links to Livelink Node ID's (basically in the LONG/Additional Notes)
	if( tableCells[j].getAttribute('colspan') == "4" &&
	    tableCells[j].getAttribute('bgcolor') == "#ffffbb")
	{
		var nodeIdRE = /(^|[^0-9])([0-9]{8})([^0-9]|$)/ig;
		
		var nodeIdContents = tableCells[j].innerHTML;

		var nodeMatches = nodeIdContents.match(nodeIdRE);

		if( nodeMatches != null )
		{
			tableCells[j].innerHTML = nodeIdContents.replace(nodeIdRE, function (str, p1, p2, p3, offset, s) {
			return p1 + p2.link("https://livelink-ca.mitra.com/Livelink/livelink.exe/fetch/2000/Livelink/livelink.exe?func=ll&objId=" + p2 + "&objAction=properties") + p3; });
		}
	}

}

